* Set working directory to folder nameds *
cd ""

* Sexism Index by State *
use "state level.dta", replace

keep state_name SEXISM
order state_name SEXISM
gsort - SEXISM
gen id = _n

* Figures 3A and 3B *
use "Figure 3 data.dta", clear

reg SEXISM ques1

reg SEXISM_rank ques1_rank

reg SEXISM ques2

reg SEXISM_rank ques2_rank

* Table 2: Summary Statistics at the Media Market Level *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
drop if missing(dmacode)
keep clinton_2party_vote_share SEXISM obama_vote_share female_vote_share trump_demo flip kerry_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS

order clinton_2party_vote_share SEXISM obama_vote_share female_vote_share trump_demo flip kerry_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS

// replace trump_demo =. if missing(per_college_1316) | missing(per_nonwhite_1316) | missing(obama_vote_share)

sum clinton_2party_vote_share SEXISM obama_vote_share female_vote_share trump_demo flip kerry_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS
reg SEXISM SEXISM
outreg2 using table2.doc, replace sum(log)

* Table 3: Predictors of Sexism *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg SEXISM flip, vce(cluster state)
outreg2 using table3.doc, replace ctitle(1) keep(flip)

reg SEXISM flip##c.(per_over60_1316 per_nonwhite_1316 per_college_1316), vce(cluster state)
outreg2 using table3.doc, append ctitle(1) 

reg SEXISM flip##c.( per_over60_1316 per_nonwhite_1316 per_college_1316) i.division, vce(cluster state)
outreg2 using table3.doc, append ctitle(1) 

* Table 4: Sexism and Clinton's Vote Share *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}

reg clinton_2party_vote_share obama_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table4.doc, replace ctitle(1) keep(c.SEXISM flip obama_vote_share kerry_vote_share)

reg clinton_2party_vote_share obama_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table4.doc, append ctitle(1) keep(c.SEXISM flip per_over60_1316 per_nonwhite_1316 per_college_1316)

reg clinton_2party_vote_share kerry_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table4.doc, append ctitle(1) keep(c.SEXISM flip obama_vote_share kerry_vote_share)

reg clinton_2party_vote_share kerry_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table4.doc, append ctitle(1) keep(c.SEXISM flip per_over60_1316 per_nonwhite_1316 per_college_1316)

reg clinton_2party_vote_share obama_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table4.doc, append ctitle(1) keep(c.SEXISM##flip )

reg clinton_2party_vote_share obama_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table4.doc, append ctitle(1) keep(c.SEXISM##flip per_over60_1316 per_nonwhite_1316 per_college_1316)

reg clinton_2party_vote_share kerry_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table4.doc, append ctitle(1) keep(c.SEXISM##flip )

reg clinton_2party_vote_share kerry_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table4.doc, append ctitle(1) keep(c.SEXISM##flip per_over60_1316 per_nonwhite_1316 per_college_1316)

* Table 5: Turnout Results *
use "Turnout data.dta", replace
	
reg ln_turnout_2016 ln_turnout_2012 c.SEXISM  unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS i.division, vce(cluster state)
outreg2 using table5.doc, replace ctitle(1) keep(SEXISM  SEXISM_sq home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 trump_demo SEXISM_trump SEXISM_trump_sq ln_turnout_2012 c.SEXISM##c.obama_vote_share)

reg ln_turnout_2016 ln_turnout_2012 c.SEXISM  unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS per_over60_1316 per_nonwhite_1316 per_college_1316 i.division, vce(cluster state)
outreg2 using table5.doc, append ctitle(1) keep(SEXISM  SEXISM_sq home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 trump_demo SEXISM_trump SEXISM_trump_sq ln_turnout_2012 c.SEXISM##c.obama_vote_share trump_demo)

reg ln_turnout_2016 ln_turnout_2012 c.SEXISM##c.obama_vote_share  unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS i.division, vce(cluster state)
outreg2 using table5.doc, append ctitle(1) keep(SEXISM SEXISM_sq  home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 trump_demo SEXISM_trump SEXISM_trump_sq ln_turnout_2012 c.SEXISM##c.obama_vote_share)

reg ln_turnout_2016 ln_turnout_2012 c.SEXISM##c.obama_vote_share  unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS per_over60_1316 per_nonwhite_1316 per_college_1316 i.division, vce(cluster state)
outreg2 using table5.doc, append ctitle(1) keep(SEXISM SEXISM_sq  home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 trump_demo SEXISM_trump SEXISM_trump_sq ln_turnout_2012 c.SEXISM##c.obama_vote_share trump_demo)

reg ln_turnout_2016 ln_turnout_2012 c.SEXISM##c.kerry_vote_share unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS i.division, vce(cluster state)
outreg2 using table5.doc, append ctitle(1) keep(SEXISM SEXISM_sq home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 trump_demo SEXISM_trump SEXISM_trump_sq ln_turnout_2012 c.SEXISM##c.kerry_vote_share)

reg ln_turnout_2016 ln_turnout_2012 c.SEXISM##c.kerry_vote_share unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 DEFINITIONS per_over60_1316 per_nonwhite_1316 per_college_1316 i.division, vce(cluster state)
outreg2 using table5.doc, append ctitle(1) keep(SEXISM SEXISM_sq home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 trump_demo SEXISM_trump SEXISM_trump_sq ln_turnout_2012 c.SEXISM##c.kerry_vote_share trump_demo)

* Table 6: Sexism Before and After Announcement of Trump Campaign *
use "Twitter data.dta", replace

areg SEXISM c.c_tweet i.year, absorb(dmacode) vce(cluster state)
outreg2 using table6.doc, replace ctitle(1) keep(c.c_tweet##flip c.fem##flip mal)

areg SEXISM c.fem i.year, absorb(dmacode) vce(cluster state)
outreg2 using table6.doc, append ctitle(1) keep(c.c_tweet##flip c.fem##flip mal)

areg SEXISM c.fem i.year mal, absorb(dmacode) vce(cluster state)
outreg2 using table6.doc, append ctitle(1) keep(c.c_tweet##flip c.fem##flip mal)

areg SEXISM c.c_tweet##flip i.year, absorb(dmacode) vce(cluster state)
outreg2 using table6.doc, append ctitle(1) keep(c.c_tweet##flip c.fem##flip mal)

areg SEXISM c.fem##flip i.year, absorb(dmacode) vce(cluster state)
outreg2 using table6.doc, append ctitle(1) keep(c.c_tweet##flip c.fem##flip mal)

areg SEXISM c.fem##flip mal i.year, absorb(dmacode) vce(cluster state)
outreg2 using table6.doc, append ctitle(1) keep(c.c_tweet##flip c.fem##flip mal)

* Table 7: Estimated Impact of Sexism, Cost to Clinton *

* Table 4 column 1 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party obama_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* Table 4 column 2 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party obama_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* Table 4 column 3 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}

reg clinton_2party kerry_2party c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)

replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* Table 4 column 4  electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}

reg clinton_2party kerry_2party c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* Table 4 column 5 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}

reg clinton_2party obama_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* Table 4 column 6 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
reg clinton_2party obama_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* Table 4 column 7 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_vote_share2 kerry_2party c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* Table 4 column 8 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_vote_share2 kerry_2party c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* Table 4 column 1 state cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
reg clinton_2party obama_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained) 

keep state_name gained clinton_2party y_hat electorals state_name2 error lb
gen diff = y_hat - clinton_2party
sort gained diff
tostring electorals, gen(electorals1)
replace electorals1 = "(" + electorals1 + ")"

gen won_before = 1 if clinton_2party > .5
replace won_before = 0 if missing(won_before)

gen won_after = 1 if y_hat > .5
replace won_after = 0 if missing(won_after)

gen electorals_before = electorals * won_before
gen electorals_after = electorals * won_after

replace state_name = state_name + electorals1
order state_name2 clinton_2party y_hat diff error electorals_before electorals_after
sort state_name2 

keep if won_before == 0 & won_after == 1

gen won_lb = 1 if lb >=.5
replace won_lb = 0 if missing(won_lb)

order state_name won_lb
gen elector_lb = electorals * won_lb

tab state_name2

* Table 4 column 2 state cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
reg clinton_2party obama_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained) 

keep state_name gained clinton_2party y_hat electorals state_name2 error lb
gen diff = y_hat - clinton_2party
sort gained diff
tostring electorals, gen(electorals1)
replace electorals1 = "(" + electorals1 + ")"

gen won_before = 1 if clinton_2party > .5
replace won_before = 0 if missing(won_before)

gen won_after = 1 if y_hat > .5
replace won_after = 0 if missing(won_after)

gen electorals_before = electorals * won_before
gen electorals_after = electorals * won_after

replace state_name = state_name + electorals1
order state_name2 clinton_2party y_hat diff error electorals_before electorals_after
sort state_name2 

keep if won_before == 0 & won_after == 1

gen won_lb = 1 if lb >=.5
replace won_lb = 0 if missing(won_lb)

order state_name won_lb
gen elector_lb = electorals * won_lb

tab state_name2


* Table 4 column 3 state cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party kerry_2party c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained) 

keep state_name gained clinton_2party y_hat electorals state_name2 error lb
gen diff = y_hat - clinton_2party
sort gained diff
tostring electorals, gen(electorals1)
replace electorals1 = "(" + electorals1 + ")"

gen won_before = 1 if clinton_2party > .5
replace won_before = 0 if missing(won_before)

gen won_after = 1 if y_hat > .5
replace won_after = 0 if missing(won_after)

gen electorals_before = electorals * won_before
gen electorals_after = electorals * won_after

replace state_name = state_name + electorals1
order state_name2 clinton_2party y_hat diff error electorals_before electorals_after
sort state_name2 

keep if won_before == 0 & won_after == 1

gen won_lb = 1 if lb >=.5
replace won_lb = 0 if missing(won_lb)

order state_name won_lb
gen elector_lb = electorals * won_lb

tab state_name2


* Table 4 column 4 Panel A electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}

reg clinton_2party kerry_2party c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained) 

keep state_name gained clinton_2party y_hat electorals state_name2 error lb
gen diff = y_hat - clinton_2party
sort gained diff
tostring electorals, gen(electorals1)
replace electorals1 = "(" + electorals1 + ")"

gen won_before = 1 if clinton_2party > .5
replace won_before = 0 if missing(won_before)

gen won_after = 1 if y_hat > .5
replace won_after = 0 if missing(won_after)

gen electorals_before = electorals * won_before
gen electorals_after = electorals * won_after

replace state_name = state_name + electorals1
order state_name2 clinton_2party y_hat diff error electorals_before electorals_after
sort state_name2 

keep if won_before == 0 & won_after == 1

gen won_lb = 1 if lb >=.5
replace won_lb = 0 if missing(won_lb)

order state_name won_lb
gen elector_lb = electorals * won_lb

tab state_name2

* Table 4 column 5 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party obama_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained) 

keep state_name gained clinton_2party y_hat electorals state_name2 error lb
gen diff = y_hat - clinton_2party
sort gained diff
tostring electorals, gen(electorals1)
replace electorals1 = "(" + electorals1 + ")"

gen won_before = 1 if clinton_2party > .5
replace won_before = 0 if missing(won_before)

gen won_after = 1 if y_hat > .5
replace won_after = 0 if missing(won_after)

gen electorals_before = electorals * won_before
gen electorals_after = electorals * won_after

replace state_name = state_name + electorals1
order state_name2 clinton_2party y_hat diff error electorals_before electorals_after
sort state_name2 

keep if won_before == 0 & won_after == 1

gen won_lb = 1 if lb >=.5
replace won_lb = 0 if missing(won_lb)

order state_name won_lb
gen elector_lb = electorals * won_lb

tab state_name2


* Table 4 column 6 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party obama_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained) 

keep state_name gained clinton_2party y_hat electorals state_name2 error lb
gen diff = y_hat - clinton_2party
sort gained diff
tostring electorals, gen(electorals1)
replace electorals1 = "(" + electorals1 + ")"

gen won_before = 1 if clinton_2party > .5
replace won_before = 0 if missing(won_before)

gen won_after = 1 if y_hat > .5
replace won_after = 0 if missing(won_after)

gen electorals_before = electorals * won_before
gen electorals_after = electorals * won_after

replace state_name = state_name + electorals1
order state_name2 clinton_2party y_hat diff error electorals_before electorals_after
sort state_name2 

keep if won_before == 0 & won_after == 1

gen won_lb = 1 if lb >=.5
replace won_lb = 0 if missing(won_lb)

order state_name won_lb
gen elector_lb = electorals * won_lb

tab state_name2


* Table 4 column 7 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}

reg clinton_vote_share2 kerry_2party c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained) 

keep state_name gained clinton_2party y_hat electorals state_name2 error lb
gen diff = y_hat - clinton_2party
sort gained diff
tostring electorals, gen(electorals1)
replace electorals1 = "(" + electorals1 + ")"

gen won_before = 1 if clinton_2party > .5
replace won_before = 0 if missing(won_before)

gen won_after = 1 if y_hat > .5
replace won_after = 0 if missing(won_after)

gen electorals_before = electorals * won_before
gen electorals_after = electorals * won_after

replace state_name = state_name + electorals1
order state_name2 clinton_2party y_hat diff error electorals_before electorals_after
sort state_name2 

keep if won_before == 0 & won_after == 1

gen won_lb = 1 if lb >=.5
replace won_lb = 0 if missing(won_lb)

order state_name won_lb
gen elector_lb = electorals * won_lb

tab state_name2

* Table 4 column 8 electoral cost *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_vote_share2 kerry_2party c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained) 

keep state_name gained clinton_2party y_hat electorals state_name2 error lb
gen diff = y_hat - clinton_2party
sort gained diff
tostring electorals, gen(electorals1)
replace electorals1 = "(" + electorals1 + ")"

gen won_before = 1 if clinton_2party > .5
replace won_before = 0 if missing(won_before)

gen won_after = 1 if y_hat > .5
replace won_after = 0 if missing(won_after)

gen electorals_before = electorals * won_before
gen electorals_after = electorals * won_after

replace state_name = state_name + electorals1
order state_name2 clinton_2party y_hat diff error electorals_before electorals_after
sort state_name2 

keep if won_before == 0 & won_after == 1

gen won_lb = 1 if lb >=.5
replace won_lb = 0 if missing(won_lb)

order state_name won_lb
gen elector_lb = electorals * won_lb

tab state_name2

* Table 8: Robustness Checks *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
* baseline model *
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, replace ctitle(1) keep(SEXISM)

* below 1000 dropped *
reg clinton_2party_vote_share SEXISM_dropped home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(2) keep(SEXISM_dropped)

* control for %stein *
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 stein_vote_share flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(3) keep(SEXISM)

* clinton 2016_total as dependent variable *
reg clinton_total_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(4) keep(SEXISM)

* clinton + stein as dependent variable *
gen clinton_stein = clinton_total_vote_share + stein_vote_share
reg clinton_stein SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(5) keep(SEXISM)

* control for swing state *
gen swing_state = 0
local states `" CO FL IA MI MN OH NV NH NC PA VA WI "'
foreach state of local states {
	replace swing_state = 1 if state_name == "`state'"
	}
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip swing_state i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(6) keep(SEXISM)

* clinton - obama as dependent *
gen difference = clinton_2party_vote_share - obama_vote_share
reg difference SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip  i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(7) keep(SEXISM)

* 4th order polynomial *
gen obama_2 = obama_vote_share^2
gen obama_3 = obama_vote_share^3
gen obama_4 = obama_vote_share^4

reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip obama_2 obama_3 obama_4  i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(8) keep(SEXISM)

* only use kerrry as a control *
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip kerry_vote_share  i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(9) keep(SEXISM)

* use obama and kerry as control
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip kerry_vote_share  i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(10) keep(SEXISM)

* use obama and obama - kerry as control
gen difference2 = obama_vote_share - kerry_vote_share
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip difference2 i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(11) keep(SEXISM)

* control for racism *
reg clinton_2party_vote_share c.SEXISM##c.RACISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(12) keep(SEXISM)

* control for islamophobia *
reg clinton_2party_vote_share c.SEXISM##c.MUSLIMS home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(13) keep(SEXISM)

* use a monthly cross sectional *
reg clinton_2party_vote_share SEXISM2 home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
outreg2 using table8.doc, append ctitle(14) keep(SEXISM2)

*** cost of sexism ***

* baseline *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* drop below 1000 *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share SEXISM_dropped home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM_dropped = -5.069551

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* control for %stien *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip stein_vote_share i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* use clinton total as dependent variable *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_total_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_total_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* use clinton - stien as dependent variable *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen clinton_stein = clinton_total_vote_share + stein_vote_share
reg clinton_stein SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_stein
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* control for swing state *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen swing_state = 0
local states `" CO FL IA MI MN OH NV NH NC PA VA WI "'
foreach state of local states {
	replace swing_state = 1 if state_name == "`state'"
	}

reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip swing_state i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* clinton-obama as dependent *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen difference = clinton_2party_vote_share - obama_vote_share
reg difference SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - difference
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* Include 4th order polynomial for %Obama *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen obama_2 = obama_vote_share^2
gen obama_3 = obama_vote_share^3
gen obama_4 = obama_vote_share^4

reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 obama_2 obama_3 obama_4 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* only use kerry as a control *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip kerry_vote_share  i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* obama + kerry as control variables *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip kerry_vote_share i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* obama, obama - kerry as control *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen difference2 = obama_vote_share - kerry_vote_share
reg clinton_2party_vote_share SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip difference2 i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* control for racism and racism * sexism *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen SEXISM_RACISM = SEXISM * RACISM
reg clinton_2party_vote_share SEXISM RACISM SEXISM_RACISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153
replace SEXISM_RACISM = SEXISM * RACISM

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum


* control for islamophobia and islamophobia * sexism *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen SEXISM_MUSLIMS = SEXISM * MUSLIMS
reg clinton_2party_vote_share SEXISM MUSLIMS SEXISM_MUSLIMS home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153
replace SEXISM_MUSLIMS = SEXISM * MUSLIMS

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* use monthly cross sectional *
use "DMA level 2.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}

reg clinton_2party_vote_share SEXISM2 home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_2partyvotes_2016], vce(cluster state)
replace SEXISM2 = -3.758896

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* electoral cost *

* baseline *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* drop below 1000 *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
* normalizes the sexism index with the words dropped by Z-score *
egen t_mean = mean(_1_SEXISM_drop)
egen t_sd = sd(_1_SEXISM_drop)
replace _1_SEXISM_drop = (_1_SEXISM_drop - t_mean)/t_sd

reg clinton_2party _1_SEXISM_drop home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_votes_2016], vce(cluster state)
replace _1_SEXISM_drop = -2.996279

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* control for stien *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip per_stein i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* use clinton total as dependent variable *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_vote_share2 SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen diff = y_hat - clinton_vote_share2
gen new_trump = trump_vote_share2 - diff

gen gained = 1 if clinton_vote_share2 <trump_vote_share2 & y_hat > new_trump
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_vote_share2 <trump_vote_share2 & lb > new_trump
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_vote_share2 <trump_vote_share2 & ub > new_trump
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* control for swing states *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen swing_state = 0
local states `" CO FL IA MI MN OH NV NH NC PA VA WI "'
foreach state of local states {
	replace swing_state = 1 if state_name == "`state'"
	}

reg clinton_2party SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip swing_state i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.136671

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* 4th order polynomial *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen obama_2 = obama_vote_share^2
gen obama_3 = obama_vote_share^3
gen obama_4 = obama_vote_share^4	

reg clinton_2party SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip obama_2 obama_3 obama_4 i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.066507

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* only control for kerry *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  kerry_2party per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.066507

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* obama & kerry as controls *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen difference2 = obama_vote_share - kerry_2party
reg clinton_2party SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS kerry_2party obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip kerry_2party i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.066507

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* obama and obama - kerry as controls *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen difference2 = obama_vote_share - kerry_2party
reg clinton_2party SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS kerry_2party obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip difference2 i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.066507

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* control for racism and racism * sexism *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen SEXISM_RACISM = SEXISM * RACISM
reg clinton_2party SEXISM RACISM SEXISM_RACISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.066507
replace SEXISM_RACISM = SEXISM * RACISM

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* control for islamophobia and islamophobia * sexism *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
gen SEXISM_MUSLIMS = SEXISM * MUSLIMS
reg clinton_2party SEXISM MUSLIMS SEXISM_MUSLIMS home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM = -3.066507
replace SEXISM_MUSLIMS = SEXISM * MUSLIMS

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum

* use monthly cross sectional *
use "state level.dta", replace

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party SEXISM2 home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_votes_2016], vce(cluster state)
replace SEXISM2 = -2.623325

predict y_hat, xb
predict error, stdp
gen lb = y_hat - invnormal(0.975)*error
gen ub = y_hat + invnormal(0.975)*error
gen state_name2 = state_name

gen gained = 1 if clinton_2party <.5 & y_hat > .5
replace gained = 0 if missing(gained)

gen gained_lb = 1 if clinton_2party <.5 & lb > .5
replace gained_lb = 0 if missing(gained_lb)

gen gained_ub = 1 if clinton_2party <.5 & ub > .5
replace gained_lb = 0 if missing(gained_ub)

gen elec_gained = electorals * gained
gen elec_lb = electorals * gained_lb
gen elec_ub = electorals * gained_ub

collapse (sum) elec_gained elec_lb elec_ub
sum







* Table 4 column 1 percentage cost *
use "DMA level 2.dta", replace
gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}

reg clinton_2party_vote_share obama_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153
predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* Table 4 column 2 percentage cost *
use "DMA level 2.dta", replace
gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share obama_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153
predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* Table 4 column 3 percentage cost *
use "DMA level 2.dta", replace
gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share kerry_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153
predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* Table 4 column 4 percentage cost *
use "DMA level 2.dta", replace
gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share kerry_vote_share c.SEXISM flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* Table 4 column 5 *
use "DMA level 2.dta", replace
gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share obama_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* Table 4 column 6 percentage cost *
use "DMA level 2.dta", replace
gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share obama_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153
replace SEXISM_trump = SEXISM * trump_demo

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* Table 4 column 7 percentage cost *
use "DMA level 2.dta", replace
gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share kerry_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153
replace SEXISM_trump = SEXISM * trump_demo

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
sum

* Table 4 column 8 percentage cost *
use "DMA level 2.dta", replace
gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party_vote_share kerry_vote_share c.SEXISM##flip female_vote_share home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 per_female_1316 per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 DEFINITIONS i.division [aweight = total_2partyvotes_2016], vce(cluster state)

replace SEXISM = -4.702153
replace SEXISM_trump = SEXISM * trump_demo

predict y_hat, xb
predict error, stdp

gen change = y_hat - clinton_2party_vote_share
replace change = change * 100
replace error = error * 100

collapse (mean) change error [aweight = total_2partyvotes_2016]
summarize







*** level of sexism where clinton would have won ***
use "state level.dta", replace
egen state = group(state_name)

gen flip = 0
local states `" FL IA MI OH WI PA "'
foreach st of local states {
	replace flip = 1 if state_name == "`st'"
	}
	
reg clinton_2party SEXISM home_state unemp_rate_13 unemp_rate_14 unemp_rate_15 unemp_rate_16 female_vote_share DEFINITIONS  obama_vote_share per_female_1316 per_over60_1316 per_nonwhite_1316 per_college_1316 flip i.division [aweight = total_votes_2016], vce(cluster state)

gen state_name2 = state_name

forval i = 1/99 {
	local j = 100 - `i'
	egen p`j' = pctile(SEXISM), p(`j')
	
	replace SEXISM = p`j' if SEXISM > p`j'
	
	predict y_hat`j', xb

	gen gained`j' = 1 if clinton_2party <.5 & y_hat`j' > .5
	replace gained`j' = 0 if missing(gained`j')

	gen elec_gained`j' = electorals * gained`j'
	}
save "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles.dta", replace

forval i = 1/99 {
	use "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles.dta", clear
	keep p`i'
	rename p`i' value
	gen percentile = `i'
	save "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\\`i'.dta", replace
	}

use "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\1.dta", replace

forval i = 2/99 {
	append using "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\\`i'.dta"
	}
duplicates drop value percentile, force
save "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\final.dta", replace

* identify states *
forval i = 1/86 {
	use "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles.dta", clear
	dis "`i'"
	keep elec_gained`i' state_name
	drop if elec_gained`i' == 0
	
	gen states = ""
	levelsof state_name, local(levels)
	foreach level of local levels {
		replace states = states + "`level' "
		}
	replace states = trim(states)
	keep states
	duplicates drop states, force
	gen percentile = `i'
	save "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\\state `i'.dta", replace
	}

use "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\state 1.dta", replace

forval i = 2/86 {
	append using "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\\state `i'.dta"
	}
	
duplicates drop percentile states, force
save "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\final states.dta", replace


* final construction *
use "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles.dta", replace

collapse (sum) elec_gained*

gen i = _n
gen _mi_miss = 0
mi reshape long elec_gained, i(i) j(j)

gsort - j
drop i
rename j percentile
drop _mi_miss

merge 1:1 percentile using "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\final.dta"
drop _merge

merge 1:1 percentile using "F:\awei\Implicit bias and political outcomes\Data\Cleaning\Political\percentiles\final states.dta"
drop _merge

gsort - percentile

export delimited "F:\awei\Implicit bias and political outcomes\Results\percentiles.csv", replace




